package com.chenchao.net;

import java.io.*;
import java.net.Socket;

/**
 * @encoding : UTF-8
 * @Time : 2020/11/24 20:43
 * @Author : Chen Chao
 * @File : javaSE.com.chenchao.net.Test05C.java
 * @Software : IntelliJ IDEA
 * @since : 1.8
 * 多线程上传客户端
 */
public class Test05C implements Runnable{
    int no;

    public static void main(String[] args)  {
        new Thread(new Test05C(1)).start();
        new Thread(new Test05C(2)).start();
        new Thread(new Test05C(3)).start();
        new Thread(new Test05C(4)).start();
        new Thread(new Test05C(5)).start();

    }

    @Override
    public void run() {
        try {
            /*创建socket对象*/
            Socket socket = new Socket("127.0.0.1",8080);
            OutputStream outputStream = socket.getOutputStream();
            /*读入文件并写入到socket*/
            File file = new File("myjava01/src/img/gg.jpg");
            FileInputStream fis = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                outputStream.write(bytes,0,len);
            }
            socket.shutdownOutput();

            /*从socket中读出传回的消息*/
            InputStreamReader isr = new InputStreamReader(socket.getInputStream());
            char[] chars = new char[1024];
            while ((len=isr.read(chars))!=-1){
                System.out.println(chars);
            }

            /*关闭资源*/
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Test05C(int no) {
        this.no = no;
    }
}
